﻿var ad_myDate = new Date();
var ad_months = ad_myDate.getMonth() + 1;
var ad_NewDate = ad_myDate.getFullYear().toString() + "-" + ad_months + "-" + ad_myDate.getDate().toString();

//读取Cookie
function ad_getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))

        return (arr[2]);
    else
        return null;
}
//过期时间转换。
function ad_getsec(str) {
    var str1 = str.substring(1, str.length) * 1;
    var str2 = str.substring(0, 1);
    if (str2 == "s") {
        return str1 * 1000;
    }
    else if (str2 == "h") {
        return str1 * 60 * 60 * 1000;
    }
    else if (str2 == "d") {
        return str1 * 24 * 60 * 60 * 1000;
    }
}
//设置cookie
function ad_setCookie(name, value, time) {
    var strsec = ad_getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec * 1);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//判断当前ad_cookie1,ad_cookie2,ad_cookie3cookie是否存在
var ad_cookie1 = ad_getCookie("ad_c1");
var ad_cookie2 = ad_getCookie("ad_c2");
var ad_cookie3 = ad_getCookie("ad_c3"); 
var cookiecount = 0;
if (ad_cookie1 != null) {
    cookiecount = 1;
}
if (ad_cookie2 != null) {
    cookiecount = 2;
}
if (ad_cookie3 != null) {
    cookiecount = 3;
}

if (cookiecount == 1) {
    //判断ad_cookie1的时间是否等于当前时间，如果等于。则返回空
    if (ad_cookie1 != ad_NewDate) {
        ad_setCookie("ad_c2", ad_NewDate, "d90");
    }
} else if (cookiecount == 2) {
    if (ad_cookie2 != ad_NewDate) {
        ad_setCookie("ad_c3", ad_NewDate, "d90");
    }
} else if (cookiecount == 3) {
    if (ad_cookie3 != ad_NewDate) {
        //数据移位 ad_cookie1=ad_cookie2的值，ad_cookie2=ad_cookie3的值，ad_cookie3=当前值
        ad_setCookie("ad_c1", ad_cookie2, "d90");
        ad_setCookie("ad_c2", ad_cookie3, "d90");
        ad_setCookie("ad_c3", ad_NewDate, "d90");
    }
} else {
    //ad_cookie1,ad_cookie2,ad_cookie3为空。
    ad_setCookie("ad_c1", ad_NewDate, "d90");
}